package com.shunwang.tools.httpclientsupport.support;

import org.apache.commons.httpclient.NameValuePair;

import com.shunwang.tools.httpclientsupport.exception.ExceptionTranslator;
import com.shunwang.tools.httpclientsupport.pool.Pool;
import com.shunwang.tools.httpclientsupport.translator.OrmTranslator;

public class HttpClientWithTTTemplate extends HttpClientWithTransformTemplate{

	private OrmTranslator ormTranslator;
	
	private ExceptionTranslator exceptionTranslator;
	
	public HttpClientWithTTTemplate(HttpClientExecutor executor) {
		super(executor);
	}

	public HttpClientWithTTTemplate(Pool pool) {
		super(pool);
	}
	/**
	 * 执行相关http请求
	 * @param obj
	 * @return
	 */
	public Object executeObject(final Object obj){
		String content = super.execute(obj);
		return buildResultDef(content);
	}

	/**
	 * 执行http请求
	 * @param pair httpclient param
	 * @return 
	 */
	public Object executeObject(NameValuePair pair){
		String content = super.execute(pair);
		return buildResultDef(content);
	}
	/**
	 * 执行http请求
	 * @param pairs httpclient params
	 * @return
	 */
	public Object executeObject(NameValuePair[] pairs){
		String content = super.execute(pairs);
		return buildResultDef(content);
	}
	
	
	
	/**
	 * 构建缺省的class对象
	 * @param obj
	 * @return
	 */
	private Object buildResultDef(String content) {
		if(ormTranslator == null){
			if(hasThrows){
				throw new NullPointerException("transform can't configure when HttpClientWithStringTemplate invoke");
			}
			if(logger.isInfoEnabled()){
				logger.info("ormTranslator can't configure when HttpClientWithTTTemplate invoke");
				return null;
			}
		}
		Object value = ormTranslator.tanslator(content);
		exceptionTranslator.translate(value);
		return value;
	}
	
	
	// --getter setter -/
	public OrmTranslator getOrmTranslator() {
		return ormTranslator;
	}

	public void setOrmTranslator(OrmTranslator ormTranslator) {
		this.ormTranslator = ormTranslator;
	}

	public ExceptionTranslator getExceptionTranslator() {
		return exceptionTranslator;
	}

	public void setExceptionTranslator(ExceptionTranslator exceptionTranslator) {
		this.exceptionTranslator = exceptionTranslator;
	}
}
